package com.yangjun.controller.usermanage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.yangjun.dao.mybatis.mapper.usermanage.UserManageMapper;
import com.yangjun.entity.usermanage.UsersEntity;
import com.yangjun.utils.AjaxJson;

@Controller
@RequestMapping("/login")
public class LoginController {
	
	@Autowired
	private UserManageMapper mapper;
	
	
	
	/**
	 * 
	 * http://192.168.7.32:8080/login/getmenu?username=ljh&password=123456
	 * http://192.168.7.32:8080/login/getmenu?username=chenxiaoyan@360kad.com&password=360kad
	 * 根据用户名密码获取用户菜单信息
	 * @param response
	 * @param username
	 * @param password
	 * @return
	 */
	 @RequestMapping("/getmenu")
	 @ResponseBody
	 public AjaxJson getMenuList(HttpServletResponse response,
			 @RequestParam(value = "username",required = true)String username,
			 @RequestParam(value = "password",required = true)String password){
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 
		 AjaxJson j = new AjaxJson();
		 
		 System.err.println("username=="+username);
//		 System.err.println("password=="+password);
		 List<String> userMenuList = mapper.getUserMenuList(username,password);
		 if(userMenuList==null || userMenuList.size()==0){
			 j.setSuccess(false);
			 j.setMsg("帐号或者密码错误 | 权限不够");
			 return j;
		 }
		 UsersEntity user = mapper.getCurrentUserInfo(username,password);
		 List<String> userReportMenuList = mapper.getUserReportMenuList(username);
		 
		 if(userReportMenuList!=null && userReportMenuList.size()>0){
			 userMenuList.add("下载列表");
			 userMenuList.add("我的报表");
		 }
		 userMenuList.addAll(userReportMenuList);
		 
		 Map<String, Object> attributes = Maps.newHashMap();
		 attributes.put("menu", userMenuList);
		 attributes.put("userid", user.getUserid());
		 attributes.put("username", user.getUsername());
		 attributes.put("userrealname", user.getRealname());
		 j.setAttributes(attributes);
		 return j;
	 }
	 

}
